#include "mainwidget.h"
#include "homewidget.h"

#include "dm/dmdef.h"
#include "globalconfig.h"

#include <QFileDialog>


MainWidget::MainWidget(QWidget *parent)
    : AWidget{parent}
{
    logoWidget = new LogoWidget(this);
    devWidget = new DeviceWidget(this);
    sceneWidget = new SceneWidget(this);
    tabWidget = new ATabWidget(this);

    auto layout = new QGridLayout(this);

    layout->addWidget(logoWidget, 0, 0);
    layout->addWidget(devWidget, 1, 0);
    layout->addWidget(new AHLine(this), 2, 0);
    layout->addWidget(sceneWidget, 3, 0);
    layout->addWidget(tabWidget, 0, 1, 4, 1);
    layout->setColumnStretch(0, 1);
    layout->setColumnStretch(1, 9);
    layout->setSpacing(6 * ratio());

    connect(tabWidget, &ATabWidget::currentChanged, this, &MainWidget::currentChanged);

    connect(&GlobalConfig::getInstance(), &GlobalConfig::connected, this, [=](const DevInfo &info) {
        m_info = info;
    });

    connect(&GlobalConfig::getInstance(), &GlobalConfig::disconnected, this, &MainWidget::disconnected);

    connect(&GlobalConfig::getInstance(), &GlobalConfig::initialized, this, &MainWidget::initialized);

    connect(&GlobalConfig::getInstance(), &GlobalConfig::loaded, this, [=]() {
        currentChanged(tabWidget->currentIndex());
    });

    connect(&GlobalConfig::getInstance(), &GlobalConfig::peak, this, [=](const stCfgPeak &peak) {
        for (int i = 0; i < inputs.size(); ++i) {
            inputs.at(i)->peak(peak.mus[i]);
        }
        for (int i = 0; i < outputs.size(); ++i) {
            outputs.at(i)->peak(peak.out[i]);
        }
    });

    connect(&GlobalConfig::getInstance(), &GlobalConfig::inputNameUpdated, this, &MainWidget::inputNameUpdated);
    connect(&GlobalConfig::getInstance(), &GlobalConfig::outputNameUpdated, this, &MainWidget::outputNameUpdated);
}

MainWidget::~MainWidget()
{

}

void MainWidget::createVirtualDevice()
{
    devWidget->createVirtualDevice();
}

void MainWidget::setLogo()
{
    QString fileName = QFileDialog::getOpenFileName(this, tr("Logo File"),
                                                    "",
                                                    tr("Images (*.png *.jpg *.jpeg *.svg *.bmp *.ico)"));
    if (!fileName.isEmpty()) {
        logoWidget->setLogo(fileName);
    }
}


void MainWidget::initialized()
{
    while (tabWidget->count() > 0) {
        tabWidget->removeTab(0);
    }
    qDeleteAll(inputs);
    qDeleteAll(outputs);
    inputs.clear();
    outputs.clear();

    auto cfgDev = GlobalConfig::getInstance().cfgDev();

    tabWidget->addTab(new HomeWidget(this), tr(LABEL_HOME));

    for (uint8_t i = 0; i < cfgDev.mus.tru_ch; ++i) {
        auto widget = new IOWidget(IOWidget::Input, i + MUS_CH1, cfgDev.cfg, cfgDev.mus, this);
        widget->set(GlobalConfig::getInstance().paramGroup().MusCh[i]);
        tabWidget->addTab(widget, GlobalConfig::getInstance().cfgName().mus[i]);
        inputs.append(widget);
    }

    for (uint8_t i = 0; i < cfgDev.out.tru_ch; ++i) {
        auto widget = new IOWidget(IOWidget::Output, i + OUT_CH1, cfgDev.cfg, cfgDev.out, this);
        widget->set(GlobalConfig::getInstance().paramGroup().OutCh[i]);
        tabWidget->addTab(widget, GlobalConfig::getInstance().cfgName().out[i]);
        outputs.append(widget);
    }
}

void MainWidget::currentChanged(const int &index)
{
    auto cfgDev = GlobalConfig::getInstance().cfgDev();
    if (index == 0) {
        auto w = qobject_cast<HomeWidget*>(tabWidget->widget(index));
        if (w) {
            w->refresh();
        }
    }
    else if (index > 0 && index < cfgDev.mus.tru_ch + 1) {

        auto w = qobject_cast<IOWidget*>(tabWidget->widget(index));
        if (w) {
            w->set(GlobalConfig::getInstance().paramGroup().MusCh[index - 1]);
        }
    }
    else {
        auto w = qobject_cast<IOWidget*>(tabWidget->widget(index));
        if (w) {
            w->set(GlobalConfig::getInstance().paramGroup().OutCh[index - cfgDev.mus.tru_ch - 1]);
        }
    }
}

void MainWidget::inputNameUpdated()
{
    const auto &cfgName = GlobalConfig::getInstance().cfgName();
    const auto &cfgDev = GlobalConfig::getInstance().cfgDev();
    for (int i = 0; i < cfgDev.mus.tru_ch; ++i) {
        tabWidget->setTabText(i + 1, cfgName.mus[i]);
    }
}

void MainWidget::outputNameUpdated()
{
    const auto &cfgName = GlobalConfig::getInstance().cfgName();
    const auto &cfgDev = GlobalConfig::getInstance().cfgDev();
    for (int i = 0; i < cfgDev.out.tru_ch; ++i) {
        tabWidget->setTabText(i + 1 + cfgDev.mus.tru_ch, cfgName.out[i]);
    }
}

void MainWidget::disconnected(const DevInfo &info)
{
    if (info == m_info) {
        while (tabWidget->count() > 0) {
            tabWidget->removeTab(0);
        }
        qDeleteAll(inputs);
        qDeleteAll(outputs);
        inputs.clear();
        outputs.clear();
    }
}

